var module_collection = db.collection("module");
(function () {
    // 构造函数
    var _con = function (info, admin) {
        var self = this;
        if (info) {
            printData("正在创建新版块：", info, admin);
            self.type = "module";
            // 自有id
            self.id = "mod_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 模块名称
            self.name = info.name;
            // 创建者
            self.creater = admin || {};
            self.creater_id = admin._id;
            // 图标
            self.preview_img = info.preview_img;
            // 创建时间
            self.createtime = (new Date()).getTime();
        };
    };
    // 导出类
    module.exports = _con;

    // 创建版块
    _con.prototype.save = co.wrap(function* () {
        var self = this;
        var _mod = self.name ? yield mongo.findOne(module_collection, {
            query: {
                name: self.name
            }
        }) : null;
        if (_mod) {
            return printError("版块已存在");
        };
        yield mongo.insertOne(module_collection, self);
        return yield mongo.findOne(module_collection, {
            query: {
                name: self.name
            }
        });
    });

    // 获取所有模块
    _con.prototype.getAll = co.wrap(function* () {
        return yield mongo.find(module_collection, { query: {} });
    });
    // 更新版块
    _con.prototype.update = co.wrap(function* (info) {
        printData("正在更新版快信息：", info);
        var _id = info._id;
        delete info._id;
        info.updatetime = (new Date()).getTime();
        var doc = yield mongo.findOneAndUpdate(module_collection, {
            query: {
                _id: new ObjectID(_id)
            },
            data: info
        });
        return doc.value;
    });
    // 根据分页信息获取对应版块列表
    _con.prototype.getListByPage = co.wrap(function* (info) {
        printData("正在获取版块列表：", info);
        try{

        var docs = yield mongo.find(module_collection, {
            skip: info.num * info.page,
            limit: info.num,
            sort: {
                createtime: -1
            }
        });
        }catch(e){
            console.log("what? ",e)
        }
        console.log(docs)
        return docs;
    });
    // 删除指定版块
    _con.prototype.delete = co.wrap(function* (_id) {
        printData("正在删除指定板块", _id);
        var doc = yield mongo.deleteOneByObjectId(module_collection, {
            _id: _id
        });
        return doc;
    });
    // 获取版块总数
    _con.prototype.count = function* () {
        return yield mongo.count(module_collection);
    };

    // 获取版块详情
    _con.prototype.detail = co.wrap(function* (_id) {
        printData("正在获取版块详情：", _id);
        return yield mongo.findOneByObjectId(module_collection, _id);
    });

    // 创建话题的时候话题数+1,删除时-1
    _con.prototype.upgradepost = co.wrap(function* (_id, isadd) {
        printData("正在更新版块话题信息：", _id, isadd);
        var doc = yield mongo.findOneByObjectId(module_collection, _id);
        var posttotal = isadd ? parseInt(doc.posttotal || 0) + 1 : parseInt(doc.posttotal || 0) - 1;
        yield mongo.updateOneByObjectId(module_collection, {
            _id: _id,
            data: {
                posttotal: posttotal
            }
        });
    });
} ());